//
//  TextSearcherPanel.m
//  LabanBrowser
//
//  Created by -Xh- on 3/12/13.
//  Copyright (c) 2013 VNG. All rights reserved.
//

#import "TextSearcherPanel.h"
#import "StaticVariables.h"
#import "SGPageViewController.h"
#import "SGWebViewController.h"
@implementation TextSearcherPanel{
    UIImageView* backgroundView;
    int currentSearchPosition;
    int numberOfSearchResults;
}
double kSlideDuration=0.4f;
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)reloadTheme{
    int themeIndex=[[[[StaticVariables shareVariables]userDefaults]theme]intValue];
    UIColor* currentThemeColor=[[[StaticVariables shareVariables]presetThemes]objectAtIndex:themeIndex];
    [UIView animateWithDuration:0.75f animations:^{
        backgroundView.layer.backgroundColor=[currentThemeColor CGColor];
    }];

}

-(void)relocalize{
    self.searchTF.placeholder=@"Input something to search";
    [backgroundView removeFromSuperview];
    backgroundView.alpha=[[[StaticVariables shareVariables]constantValues]themeAlpha];
    int currentThemeIndex= [[[[StaticVariables shareVariables]userDefaults]theme]intValue];
    UIColor* currentThemeColor=[[[StaticVariables shareVariables]presetThemes]objectAtIndex:currentThemeIndex];
    backgroundView.layer.backgroundColor=[currentThemeColor CGColor];
    [self addSubview:backgroundView];
    [self sendSubviewToBack:backgroundView];
}

-(void)initializeWithPosititionY:(double)_y{
    CGRect curFrame=self.frame;
    curFrame.origin.y=_y;
    self.frame=curFrame;
    self.searchTF.delegate=self;
    backgroundView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    
    self.nextBT.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    self.prevBT.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    self.closeBT.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
    self.searchTF.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.resultLB.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
-(BOOL)isVisible{
    return self.frame.origin.y==0;
}

-(void)display{
    if([self isVisible]==NO){
        CGRect curFrame=self.frame;
        curFrame.origin.y=0;
        [UIView animateWithDuration:kSlideDuration
                              delay:0 options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.frame=curFrame;
                         } completion:^(BOOL finished) {
                             
                         }];
    }
    
}
-(void)hide{
    if([self isVisible]==YES){
        CGRect curFrame=self.frame;
        curFrame.origin.y=-self.frame.size.height;
        [UIView animateWithDuration:kSlideDuration
                              delay:0 options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.frame=curFrame;
                         } completion:^(BOOL finished) {
                             
                         }];
    }
}
-(void)close{
    [self hide];
    self.searchTF.text=@"";
    [self.searchTF resignFirstResponder];
    currentSearchPosition = numberOfSearchResults = 0;
    [self updateChrome];
    [self.browser removeHighlight];
    if(delegate&&[delegate respondsToSelector:@selector(onCloseSearchPanel)]){
        [delegate onCloseSearchPanel];
    }
}
- (IBAction)onClose:(id)sender {
    [self close];
    
}

- (IBAction)toPrev:(id)sender {
    if(self.searchTF.isFirstResponder){
        [self.searchTF resignFirstResponder];
    }
    int prevResult = [self.browser prevSearchResult];
    if(prevResult != -1){
        currentSearchPosition = numberOfSearchResults - prevResult;
        [self updateChrome];
    }
}

- (IBAction)toNext:(id)sender {
    if(self.searchTF.isFirstResponder){
        [self.searchTF resignFirstResponder];
    }
    int nextResult = [self.browser nextSearchResult];
    if(nextResult != -1){
        currentSearchPosition = numberOfSearchResults - nextResult;
        [self updateChrome];
    }
}

-(void)updateChrome{
    self.resultLB.text = [NSString stringWithFormat:@"%i / %i",currentSearchPosition,numberOfSearchResults];
}

-(void)updateSearchResultInfoFromDict:(NSDictionary*)infoDict{
    //self.resultLB
    if(infoDict == nil){
        self.resultLB.text = [NSString stringWithFormat:@"%i / %i",0,0];
        return;
    }
    numberOfSearchResults = [[infoDict objectForKey:@"numberOfSearchResults"]intValue];
    currentSearchPosition = [[infoDict objectForKey:@"currentSearchPosition"]intValue];
    currentSearchPosition = numberOfSearchResults - currentSearchPosition;
    [self updateChrome];
}

#pragma mark uitextFieldDelegate
-(BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString* realTextFieldValue=[_textField.text stringByReplacingCharactersInRange:range withString:string];
    if(realTextFieldValue.length>=2){
        [self updateSearchResultInfoFromDict:[self.browser findInPage:realTextFieldValue]];
    }
    
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
@end
